﻿using KZ.Express.ENT;
using KZ.Express.Presenter;
using KZ.Express.Resource;
using KZ.Utilities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace KZ.Express.Setup.PriceList
{
    public partial class ZoneDetailView : FormBase
    {
        #region Private Variables

        private object _previousValue;
        private List<DataRow> _changedRow;
        private new ZoneDetailPresenter _presenter;

        #endregion Private Variables

        #region Private Methods

        private void ZoneDetailView_Load(object sender, EventArgs e)
        {
            _changedRow = new List<DataRow>();
            LoadData();
        }

        private void dtgZoneDetail_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            e.Control.PreviewKeyDown -= Control_PreviewKeyDown;
            e.Control.PreviewKeyDown += Control_PreviewKeyDown;
        }

        private void dtgZoneDetail_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    e.SuppressKeyPress = true;
                    if (dtgZoneDetail.CurrentCell.ColumnIndex == colTemp.Index)
                    {
                        if (dtgZoneDetail.CurrentRow.Index == dtgZoneDetail.RowCount - 1)
                            break;
                        dtgZoneDetail.CurrentCell = dtgZoneDetail[colZone.Index, dtgZoneDetail.CurrentCell.RowIndex + 1];
                    }
                    else
                        SendKeys.Send("{TAB}");
                    break;

                case Keys.Delete:
                    if (dtgZoneDetail.SelectedRows != null)
                        foreach (DataGridViewRow row in dtgZoneDetail.SelectedRows)
                        {
                            row.Cells[colZone.Index].Value = DBNull.Value;
                            if (!_changedRow.Contains(base._table.Rows[row.Index]))
                                _changedRow.Add(base._table.Rows[row.Index]);
                        }
                    break;
            }
        }

        private void dtgZoneDetail_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (!dtgZoneDetail.CurrentCell.ReadOnly)
                dtgZoneDetail.BeginEdit(true);
        }

        private void dtgZoneDetail_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            _previousValue = dtgZoneDetail[e.ColumnIndex, e.RowIndex].Value;
        }

        private void dtgZoneDetail_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (_previousValue != dtgZoneDetail[e.ColumnIndex, e.RowIndex].Value)
                if (!_changedRow.Contains(base._table.Rows[e.RowIndex]))
                    _changedRow.Add(base._table.Rows[e.RowIndex]);
        }

        private void Control_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (dtgZoneDetail.CurrentCell.ColumnIndex == colZone.Index ||
                    dtgZoneDetail.CurrentCell.ColumnIndex == colTemp.Index)
                {
                    if (dtgZoneDetail.CurrentRow.Index == dtgZoneDetail.RowCount - 1)
                        return;
                }
                else
                    SendKeys.Send("{TAB}");
            }
        }

        protected override void LoadData()
        {
            try
            {
                _changedRow.Clear();
                _presenter.LoadZoneDetail();
                switch (base.Action)
                {
                    case ActionName.Insert:
                        break;

                    case ActionName.Update:
                        int zoneCount = base._table.AsEnumerable().
                            Where(p => !String.IsNullOrEmpty(p.Field<string>("Zone"))).
                            GroupBy(p => p["Zone"]).Count();
                        lblZoneCount.Text = "Số zone: " + zoneCount;
                        _presenter.LoadEntity();
                        break;
                }
            }
            catch (BusinessLogicException ex)
            {
                SetMessage(ex.Message, false);
            }
        }

        #endregion Private Methods

        #region Constructors

        public ZoneDetailView()
        {
            InitializeComponent();
            base.ObjectID = (int)Objects.Zone;

            base.OrderBy = "CountryName";
            base.OrderDirection = "ASC";

            base.InitializeDataGrid(dtgZoneDetail);
            base._presenter = _presenter = new ZoneDetailPresenter(this);

            dtgZoneDetail.ColumnHeaderMouseClick += dtg_ColumnHeaderMouseClick;
        }

        #endregion Constructors

        #region Public Properties

        [Search]
        public int ZoneID
        {
            get { return base.EntityID; }
        }

        public override EntityBase Entity
        {
            get
            {
                ZoneENT zone = new ZoneENT();
                zone.ZoneID = base.EntityID;
                zone.ZoneCode = txtZoneCode.Text;
                zone.ZoneName = txtZoneName.Text;

                return zone;
            }
            set
            {
                ZoneENT zone = value == null ? new ZoneENT() : value as ZoneENT;
                txtZoneCode.Text = zone.ZoneCode;
                txtZoneName.Text = zone.ZoneName;
            }
        }

        #endregion Public Properties

        #region Public Methods

        public override void Save()
        {
            try
            {
                dtgZoneDetail.EndEdit();
                _presenter.Save(_changedRow);
                _changedRow.Clear();
            }
            catch (BusinessLogicException ex)
            {
                SetMessage(ex.Message, false);
            }
        }

        #endregion Public Methods
    }
}